<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('证照管理') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('楼号')" prop="floor">
        <el-input
          v-model="queryParams.floor"
          :placeholder="$t('楼号')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('证照类型')" prop="licenseType">
        <el-select v-model="queryParams.licenseType" :placeholder="$t('证照类型')" clearable>
          <el-option
            v-for="dict in licenseTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('证号')" prop="certificateNumber">
        <el-input
          v-model="queryParams.certificateNumber"
          :placeholder="$t('证号')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:license:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:license:import']"
          type="info"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >{{ $t('导入') }}
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dealLicenseList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('楼号')" align="center" prop="floor" />
      <el-table-column :label="$t('证照类型')" align="center" prop="licenseType" :formatter="licenseTypeFormat" />
      <el-table-column :label="$t('证号')" align="center" prop="certificateNumber" />
      <el-table-column :label="$t('备注')" align="center" prop="remark" />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['deal:license:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['deal:license:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改证照管理对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealLicenseForm ref="dealLicenseForm" />
    </el-dialog>

    <!-- 导入 -->
    <el-dialog
      :title="$t('证照管理导入')"
      :visible.sync="upload.open"
      width="400px"
      :close-on-click-modal="false"
      :lock-scroll="false"
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport+ '&dealId=' + upload.dealId"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload" />
        <div class="el-upload__text">
          {{ $t('将文件拖到此处，或') }}
          <em>{{ $t('上传') }}</em>
        </div>
        <!--        <div slot="tip" class="el-upload__tip">-->
        <!--          <el-checkbox v-model="upload.updateSupport" /> {{ $t('是否更新已经存在的数据') }}-->
        <!--        </div>-->
        <div slot="tip" class="el-upload__tip" style="color:red">
          {{ $t('提示：仅允许导入“xls”或“xlsx”格式文件！') }}
          <el-link type="primary" style="font-size:12px" @click="fetchTemplate">
            {{ $t('下载模板') }}
          </el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button @click="upload.open = false">{{ $t("取消") }}</el-button>
        <el-button type="primary" @click="submitFileForm">{{ $t("确定") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDealLicense, delDealLicense } from '@/api/deal/license/dealLicense'
import dealLicenseForm from './dealLicenseForm.vue'
import { getToken } from '@/utils/auth'
export default {
    components: {
        dealLicenseForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 证照管理表格数据
          dealLicenseList: [],
          // 弹出层标题
          title: '证照管理',
          // 是否显示弹出层
          formOpen: false,
        // 证照类型字典
        licenseTypeOptions: [],

          // 查询参数
          queryParams: {
              dealId: this.$route.query.id,
              floor: undefined,
              licenseType: undefined,
              certificateNumber: undefined,
              landUseRightsHolder: undefined,
              located: undefined,
              placeNumber: undefined,
              drawingNumber: undefined,
              useTheRightType: undefined,
              landType: undefined,
              usageArea: undefined,
              endDate: undefined,
              rightHolder: undefined,
              roomNumber: undefined,
              entitlementType: undefined,
              rightsNature: undefined,
              use: undefined,
              area: undefined,
              usePeriod: undefined,
              postscript: undefined,
              houseOwner: undefined,
              houseLocated: undefined,
              sharedSituation: undefined,
              registrationDate: undefined,
              natureOfHouse: undefined,
              planningPurposes: undefined,
              constructionSite: undefined,
              totalNumberFloors: undefined,
              structure: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {},
          // 用户导入参数
          upload: {
            // 是否显示弹出层（用户导入）
            open: false,
            // 弹出层标题（用户导入）
            title: '',
            // 是否禁用上传
            isUploading: false,
            // 是否更新已经存在的用户数据
            updateSupport: 0,
            // dealId
            dealId: this.$route.query.id,
            // 设置上传的请求头部
            headers: { Authorization: 'Bearer ' + getToken() },
            // 上传的地址
            url: process.env.VUE_APP_BASE_API + '/license/dealLicense/importData'
          }
      }
  },
  created() {
      this.getList()
      this.getDicts('deal_license_type').then(response => {
          this.licenseTypeOptions = response.data
      })
  },
  methods: {
    /** 查询证照管理列表 */
    getList() {
      this.loading = true
      listDealLicense(this.queryParams).then(response => {
        this.dealLicenseList = response.rows
        this.total = response.total
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 证照类型字典翻译
    licenseTypeFormat(row, column) {
      console.log(column)
      return this.selectDictLabel(this.licenseTypeOptions, row.licenseType)
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            dealId: undefined,
            floor: undefined,
            licenseType: undefined,
            certificateNumber: undefined,
            landUseRightsHolder: undefined,
            located: undefined,
            placeNumber: undefined,
            drawingNumber: undefined,
            useTheRightType: undefined,
            landType: undefined,
            usageArea: undefined,
            endDate: undefined,
            rightHolder: undefined,
            roomNumber: undefined,
            entitlementType: undefined,
            rightsNature: undefined,
            use: undefined,
            area: undefined,
            usePeriod: undefined,
            postscript: undefined,
            houseOwner: undefined,
            houseLocated: undefined,
            sharedSituation: undefined,
            registrationDate: undefined,
            natureOfHouse: undefined,
            planningPurposes: undefined,
            constructionSite: undefined,
            totalNumberFloors: undefined,
            structure: undefined,
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.dealLicenseForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealLicenseForm.init(row)
        })
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = this.$t('导入')
      this.upload.open = true
    },
    // 文件上传中处理
    handleFileUploadProgress() {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.$alert(response.msg, this.$t('导入结果'), { dangerouslyUseHTMLString: true })
      this.getList()
    },
    // 下载模板
    fetchTemplate() {
      const href1 = window.location.href
      const href2 = this.$route.path
      const url = href1.substring(0, href1.indexOf(href2))
      window.location.href = url + '/template/licenseImportTemplate.xlsx'
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        return delDealLicense(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(() => {
          this.loading = false
      })
    }
  }
}
</script>
